### |- world map for background ----
world <- st_as_sf(rnaturalearth::ne_countries(scale = 'large', returnclass = 'sf'))
### |- Base map ----
# Focus on the Salish Sea region
map <- ggplot() +
# Geoms
geom_sf(data = world, fill = "lightgrey", color = "white") +
geom_sf_interactive(
data = st_as_sf(orcas_data_clean, coords = c("begin_longitude", "begin_latitude"), crs = 4326),
aes(tooltip = paste("Date:", date, "<br>Location:", location), data_id = vessel),
color = col_palette[1],
alpha = 0.5,
shape = 1,
size = 1
) +
# Scales
coord_sf(xlim = c(-127, -121), ylim = c(47, 51), expand = FALSE) + # Focus on the Salish Sea region
# Labs
labs(
title = "",
x = "Longitude",
y = "Latitude"
) +
# Theme
ggthemes::theme_map()
### |- Bar plot plot ----
# Number of Encounters per Vessel
bar <- ggplot(bar_plot_data, aes(x = vessel, -n, y = n)) +
# Geom
geom_bar_interactive(
aes(
tooltip = paste("Vessel:", vessel, "<br>Encounters:", n),
data_id = vessel),
stat = "identity",
fill = col_palette[1]
) +
# Scale
scale_x_discrete() +
scale_y_continuous() +
coord_flip() +
# Labs
labs(
x = "Vessel",
y = "Number of Encounters",
title = title_text,
subtitle = subtitle_text,
caption = caption_text
) +
# Theme
theme(
plot.title = element_text(
size = rel(1.3),
family = "title",
face = "bold",
color = title_col,
lineheight = 1.1,
margin = margin(t = 5, b = 5)
),
plot.subtitle = element_text(
size = rel(0.65),
family = "subtitle",
color = subtitle_col,
lineheight = 1.1,
margin = margin(t = 5, b = 5)
),
plot.caption = element_markdown(
size = rel(0.40),
family = "caption",
color = caption_col,
lineheight = 1.1,
hjust = 0.5,
halign = 1,
margin = margin(t = 5, b = 5)
)
)
### |- inset element ----
# Insert the map into the upper right corner of the bar plot
combined_plot <- bar +
inset_element(map, left = 0, bottom = 0.2, right = 1.3, top = 1.05)